<?php

require_once(Yii::getPathOfAlias('webroot')."/setup.php");

class ShopController extends Controller
{
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations
		);
	}
	public function allowedActions()
	{
		//return 'error';
	}
	
	public function actionIndex()
	{
		$model = new Shop('search');
		$model->unsetAttributes();  // clear any default values
		
		if(isset($_GET['Shop']))
			$model->attributes=$_GET['Shop'];

		$this->render('index',array(
			'model'=>$model,
		));
	}
	
	public function actionUpdate($id){
		$model = Shop::model()->findByPk($id);
		$model->id_category_theme =  CategoriesTheme::model()->findByPk($model->id_category_theme)->name;
		$model->city = Provinces::model()->findByPk($model->city)->title;
		$model->district = Wards::model()->findByPk($model->district)->title;
		
		$this->render('update',array(
			'model'=>$model,
		));
	}
	
	public function loadModel($id)
	{
		$model=Shop::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	public function actionDelete($id)
	{
		// delete folder domain
		$installShop = new installShop;
		$model = Shop::model()->findByPk($id);
		$domain = $model->domain;
		$installShop->parentDirectory = $installShop->parentDirectory.$domain.DIRECTORY_SEPARATOR;
		$installShop->deleteFolder($installShop->parentDirectory);
		//delete database
		$installShop->deleteDb("web_".$domain);
		
		$model->delete();
		
		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
	}
	
	// function delete rows
	public function actionDeleteAll($id){
		$arrIdNew = explode(",",$id);
		for($i=0; $i<count($arrIdNew); $i++){
		
			// delete folder domain
			$installShop = new installShop;
			$model = Shop::model()->findByPk($arrIdNew[$i]);
			$domain = $model->domain;
			$installShop->parentDirectory = $installShop->parentDirectory.$domain.DIRECTORY_SEPARATOR;
			$installShop->deleteFolder($installShop->parentDirectory);
			//delete database
			$installShop->deleteDb("web_".$domain);
			$model->delete();
		}
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
	}
}